package com.liuxvru.project.user.controller;

import com.liuxvru.common.annotation.Log;
import com.liuxvru.common.core.controller.BaseController;
import com.liuxvru.common.core.domain.AjaxResult;
import com.liuxvru.common.core.page.TableDataInfo;
import com.liuxvru.common.enums.BusinessType;
import com.liuxvru.common.utils.poi.ExcelUtil;
import com.liuxvru.project.domain.WorkAttachment;
import com.liuxvru.project.service.IWorkAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 工作项附件Controller
 *
 * @author liuxvru
 * @date 2025-02-17
 */
@RestController
@RequestMapping("/user/project/workAttachment")
public class UserWorkAttachmentController extends BaseController {
    @Autowired
    private IWorkAttachmentService workAttachmentService;

    /**
     * 查询工作项附件列表
     */
    @GetMapping("/list/{workItemId}")
    public TableDataInfo list(@PathVariable("workItemId") Long workItemId, WorkAttachment workAttachment) {
        startPage();
        workAttachment.setWorkId(workItemId);
        List<WorkAttachment> list = workAttachmentService.selectWorkAttachmentList(workAttachment);
        return getDataTable(list);
    }

    /**
     * 查询工作项附件列表
     */
    @GetMapping("/listAll/{workItemId}")
    public AjaxResult listAll(@PathVariable("workItemId") Long workItemId, WorkAttachment workAttachment) {
        workAttachment.setWorkId(workItemId);
        List<WorkAttachment> list = workAttachmentService.selectWorkAttachmentList(workAttachment);
        return success(list);
    }


    /**
     * 导出工作项附件列表
     */
    @Log(title = "工作项附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkAttachment workAttachment) {
        List<WorkAttachment> list = workAttachmentService.selectWorkAttachmentList(workAttachment);
        ExcelUtil<WorkAttachment> util = new ExcelUtil<WorkAttachment>(WorkAttachment.class);
        util.exportExcel(response, list, "工作项附件数据");
    }

    /**
     * 获取工作项附件详细信息
     */
    @GetMapping(value = "/{attachmentId}")
    public AjaxResult getInfo(@PathVariable("attachmentId") Long attachmentId) {
        return success(workAttachmentService.getById(attachmentId));
    }

    /**
     * 新增工作项附件
     */
    @Log(title = "工作项附件", businessType = BusinessType.INSERT)
    @PostMapping("/{workItemId}")
    public AjaxResult add(@RequestBody WorkAttachment workAttachment,  @PathVariable("workItemId") Long workItemId) {
        return toAjax(workAttachmentService.add(workAttachment, workItemId));
    }

    /**
     * 修改工作项附件
     */
    @Log(title = "工作项附件", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult edit(@RequestBody WorkAttachment workAttachment) {
        return toAjax(workAttachmentService.edit(workAttachment));
    }

    /**
     * 删除工作项附件
     */
    @Log(title = "工作项附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectId}/{workItemId}/{attachmentIds}")
    public AjaxResult remove(@PathVariable Long[] attachmentIds, @PathVariable("projectId") Long projectId, @PathVariable("workItemId") Long workItemId) {
        return toAjax(workAttachmentService.delFiles(projectId, workItemId, Arrays.asList(attachmentIds)));
    }
}
